//-------------------- common validation --------------------
// show message content.
	function showMessage(message){
		alert(message);	
	}
	
// popup window.
	function popupWindow(url){
		window.open(url,'','resizable=no,scrollbars=yes,x=300,y=100,width=450,height=300,left=280, top=180');
	}	
	
// popup viewItem
	function popupViewItem(no, cust_no, flag){ 
		var url = '';
		if(flag == 'survey'){
			url = "popupSurvey.jsp?svyNumber="+no;
		}else if(flag == 'result'){
			url = "popupSvyResult.jsp?svyNumber="+no+"&cust_no="+cust_no;
		}else if(flag == 'question'){
			url = 'popupQuestion.jsp?qustNo='+no;
		}
		window.open(url,'','resizable=no,scrollbars=yes,x=300,y=100,width=750,height=500,left=280, top=180');
	}	
	
//-------------------- checkbox validation --------------------
// check or uncheck all of chechbox.
	function checkboxAll(){  
		var count = '';
		var f = document.getElementById('myForm');
	// check count value
		if(f.chkbox == null){
			count = 0;
		}else if(f.chkbox.length >= 2){
			count = f.chkbox.length;
		}else{
			count = 1;
		}		
	// check or uncheck	
		if(count >= 2){
			if(f.chkboxAll.checked == true){			
				for(var i=0; i<f.chkbox.length; i++){
					f.chkbox[i].checked = true;
				}
			}else{
				for(var i=0; i<f.chkbox.length; i++){
					f.chkbox[i].checked = false;
				}
			}
		}else if(count == 1){
			if(f.chkboxAll.checked == true){
				f.chkbox.checked = true;
			}else{
				f.chkbox.checked = false;
			}
		}else if(count == 0){
			alert("삭제할 레코드가 존재하지 않습니다.");
			f.chkboxAll.checked = false;
		}					
	}

// see if the checkbox is checked or not.
	function isChkList(f){
		for(var i=0; i<f.chkbox.length; i++){
			if(f.chkbox[i].checked){
				return true;
			}
		}
		if(f.chkbox.checked){
			return true;
		}
		return false;
	}
	
// validate checkbox and get the key value for update
	function validateUpdateChkbox(f){		
		var sCount = 0;
		var key = "";
	// checkbox 유무.
		if(f.chkbox == null){
			alert("수정할 레코드가 존재하지 않습니다.");
			return;
		}
	// checkbox 클릭유무.	
		if(isChkList(f) == false){
			alert("수정할 레코드를 선택하시기 바랍니다.");
			return;
		}		
		
	// count chkbox
		var chkCount = 0;
		if(f.chkbox.length >=2){
			chkCount = f.chkbox.length;
		}else{
			chkCount = 1;
		}
									
	// 체크박스 클릭 값 가져오기			
		if(chkCount == 1){
			if(f.chkbox.checked == true){
				key = f.chkbox.value;
			}	
		}else{
			if(f.chkbox.length >= 2){
				for(var i=0; i<f.chkbox.length; i++){
					if(f.chkbox[i].checked == true){
						key = f.chkbox[i].value;
						sCount++;
					}
				}	
				if(sCount > 1){
					alert("1개의 레코드만 수정하실 수 있습니다.");
					return;
				}	
			}
		}
			
		return key;
	}
	
// validate checkbox and get the key value for delete.
	function validateRemoveChkbox(f){
		var key = "";
		
	// checkbox 유무.
		if(f.chkbox == null){
			alert("삭제할 레코드가 존재하지 않습니다.");
			return;
		}
	// checkbox 클릭유무.	
		if(isChkList(f) == false){
			alert("삭제할 레코드를 선택하시기 바랍니다.");
			return;
		}
	// 체크박스 클릭 값 가져오기
		if(f.chkbox.length >= 2){
			for(var i=0; i<f.chkbox.length; i++){
				if(f.chkbox[i].checked == true){
					key += f.chkbox[i].value+"|";
				}
			}				
		}else{
			if(f.chkbox.checked == true){
				key = f.chkbox.value;
			}
		}		
		return key;
	}

//-------------------- survey entry validation --------------------		
// get check & uncheck checkbox.
	function getSelUnSelCheckbox(f){		
		var chkSel = "";
		var unChkSel = "";
	// get check & uncheck value.		
		if(f.chkbox.length >= 2){
			for(var i=0; i<f.chkbox.length; i++){
				if(f.chkbox[i].checked == true){
					chkSel += f.chkbox[i].value + "|";
				}else if(f.chkbox[i].checked == false){
					unChkSel += f.chkbox[i].value + "|";
				}
			}
		}else{
			if(f.chkbox.checked){
				chkSel = f.chkbox.value + "|";
			}else if(f.chkbox.checked == false){
				unChkSel = f.chkbox.value + "|";
			}
		}	
	// concat chkSel, unChkSel	
		 return chkKey = (chkSel+'^'+unChkSel);												
	}	
	
/* 2013-05-10 (jeongyej) : add ExtJS 4.0 common function */	
	
/* 1x1 transparent image used by Ext to create inline icons with css BG images. */	
	Ext.BLANK_IMAGE_URL = '/extjs/resources/themes/images/default/tree/s.gif';
	
/* Ext JS QuickTips */	
	Ext.QuickTips.init();
	
/* Ajax Timeout */
	Ext.Ajax.timeout = 60000;	
	
/* global variables */	
	
	var M_CRT 					  = 'crt';			
	var M_EDIT   				  = 'edit';	
	var M_DEL    				  = 'del'; 
	var M_VIEW   				  = 'view';
	var M_LIST   				  = 'list';
	var M_SAVE   				  = 'save';
	var M_SUBMIT 				  = "submit";
	var M_EXCEL			      = 'excel';
	
	var AJAX         		  = 'ajax';
	var JSON      			  = 'json';	
	var POST							= 'post';
	
	var SUCCESS				= 'Success';
	var WARNING				= 'Warning';
	
	var EXTJS_OK      = Ext.MessageBox.OK;
	var EXTJS_INFO    = Ext.MessageBox.INFO;
	var EXTJS_WARN    = Ext.MessageBox.WARNING;
	var EXTJS_ERROR   = Ext.MessageBox.ERROR;
	
	var EXTJS_DATA_MODEL 					= 'Ext.data.Model';
	var EXTJS_GRID_PANEL 					= 'Ext.grid.Panel';
	var EXTJS_JSONSTORE  					= 'Ext.data.JsonStore';
	var EXTJS_STORE								= 'Ext.data.Store';
	var EXTJS_TOOLBAR    					= 'Ext.Toolbar';
	var EXTJS_WINDOW     					= 'Ext.Window';
	var EXTJS_FORM_PANEL 					= 'Ext.form.Panel';
	var EXTJS_BUTTON							= 'Ext.Button';
	var EXTJS_COMBOBOX						= 'Ext.form.field.ComboBox';
	
	var EXT_DATA_STORE						= 'Ext.data.Store';

/**
 * initiates object
 * 
 * @param {String} obj_id
 */
	
  function initialize(obj_id){  	
  	if(!Ext.isEmpty(obj_id)){
  		if(Ext.getCmp(obj_id) != undefined)
  			Ext.getCmp(obj_id).destroy();
  	}  	  
  }
  					
/**
 * draws ExtJS Calendar Component.
 * 
 * @param {String} obj
 * 		the value of object name. 
 * @param {String} obj_val
 *    the value of object.
 * @param {Integer}	obj_width
 * 		the value of object width
 */
	
	function draw_calendar(obj, obj_val, obj_width){
	
		Ext.create('Ext.form.field.Date', {
			id : obj+'_id',
			name : obj,
			renderTo : 'div_'+obj,			
			value : obj_val,
			format : 'Y-m-d',
			editable : false,			
			width : obj_width,			
			emptyText : 'YYYY-MM-DD'			
		});
				
	}	
	
/** show alert message box
 * 
 * @param  title
 * 		messagebox title
 * @param  msg
 * 		messagebox message
 * @param  icon
 * 		messagebox icon
 * @param  buttons
 * 		messagebox button
 * @param	  width
 * 		messagebox width
 * @param  fn
 * 		handler function
 */
	
	function showMsg(title, msg, icon, button, width, fn){
		
		Ext.MessageBox.show({
			title 	: title,
			msg 		: msg,
			icon 		: icon,
			buttons : button,			
			width 	: width,
			fn 			: fn
		});
		
	}

/** see if object exists or not. if not, return null.
 * 
 * @param  objId
 * 
 * @return object or null
 */	
	
	function gObj(objId){
		
		if(document.getElementById(objId)){
  		return document.getElementById(objId);
  	}else{
  		return null;
  	}
  	
	}
	
/** see if ExtJS component object exists or not. if not, return null.
 * 
 * @param  objId
 * 
 * @return object or null
 */	
	
	function gCmpObj(objId){
		
		if(Ext.getCmp(objId)){
			return Ext.getCmp(objId);
		}else{
			return null;	
		}
		
	}
	
/** see if ExtJS store object exists or not. if not, return null.
 * 
 * @param  objId
 * 
 * @return object or null
 */	
	
	function gStoreObj(objId){
		
		if(Ext.data.StoreManager.get(objId)){
			return Ext.data.StoreManager.get(objId);
		}else{
			return null;	
		}
		
	}

/** see if ExtJS model object exists or not. if not, return null.
 * 
 * @param  objId
 * 
 * @return object or null
 */	
	
	function gModelObj(objId){
		
		if(Ext.ModelMgr.getModel(objId)){
			return Ext.ModelMgr.getModel(objId);
		}else{
			return null;	
		}
		
	}
	
/** initialize ExtJS object
 * 
 * @param  objId
 */
	
	function initObj(objId){
		
		if(!Ext.isEmpty(objId)){
  		if(Ext.getCmp(objId) != undefined)
  			Ext.getCmp(objId).destroy();
  	}  
  	
	}
	
/** shows error message box.
 *	
 *	@param 
 */
	
	function showErrorMsg(msg, width){
		
		if(Ext.isEmpty(msg)){
			msg = '<b><font color="red">There are some errors. Please contact system administrator.</font></b>';
			width = 470;
		}
		
		showMsg('Error', msg, EXTJS_ERROR, EXTJS_OK, width, '');
		
	}
	
/* 일반 Text 를 bold 및 #0046ad 처리한다. */
	
	function getInfoTxtMsg(msg){
		return '<b><font color="#0046ad">'+msg+'</font></b>';
	}
		
/* 일반 msg 를 bold 및 red 처리한다. */
	
	function getRedTxtMsg(msg){
		return '<b><font color="red">'+msg+'</font></b>';
	}
	
/**	mask selected area with msg.
 * 
 * @param  objId
 * 		selected area.
 * @param  message
 *  	message shown in LoadMask.
 */
	
	function showMsgMask(objId, msg){
		
		var maskObjCmp = Ext.getCmp(objId);
		if(!Ext.isEmpty(maskObjCmp)){
			var mask = new Ext.LoadMask(maskObjCmp, {
				id : objId+'_mask',
				msg : msg
			});
			
			mask.show();
		}
		
	}
	
/** unmask selected area with msg.
 * 
 * @param  objId
 * 		object id where mask will be rendered.
 */	
	
	function clearMsgMask(objId){
		
		var mask = Ext.getCmp(objId+'_mask');				
		mask.destroy();
		
	}
	
/* mask on document body. */
	
	var showBodyMask = function(){
		Ext.getBody().mask();
	};
	
/* unmask on document body. */
	
	var clearBodyMask = function(){
		Ext.getBody().unmask();
	};
			
/** submit form.
 * 
 * @param  form
 * 		form object
 * @param  action
 * 		form action
 * @param  target
 * 		form target
 */	
	
	function submitForm(form, action, target){
		
		form.action = action;
		form.target = target;
		form.submit();
		
	}
	
/** draws ExtJS Calendar Component.
 * 
 * @param divId
 * 	calendar 위치.
 * @param obj
 * 	object 
 * @param value
 *  value
 * @param width
 * @param	 allowBlank	
 * 
 */
	
	function drawCalendar(divId, obj, value, width, allowBlank, fieldLabel, labelWidth, labelAlign, readOnly, fieldCls){
			
		Ext.create('Ext.form.field.Date', {
			id : obj,
			name : obj,
			renderTo : divId,
			value : value,
			format : 'Y-m-d',
			editable : false,			
			width : width,
			allowBlank : allowBlank,
			fieldLabel : fieldLabel,
			labelWidth : labelWidth,
			labelAlign : labelAlign,
			emptyText : 'YYYY-MM-DD',
			readOnly	: readOnly,
			fieldCls : fieldCls
		});
				
	}	
	
/**	add common data store.
 * 
 * @param  objId
 * @param  proxyType
 * 		the way to send params to server. etc.. ajax.
 * @param  cmdUrl
 * @param  params
 * @param  readerType
 *   	reader of store. etc.. json.
 * @param  root
 * @param  model
 * @param  groupField
 * 
 * @return {JsonStore}	commonDS
 */
	
	function getCommonDS(objId, proxyType, cmdUrl, params, readerType, root, model, groupField){
		
		var commonDS = Ext.create(EXTJS_JSONSTORE,{
			storeId : objId,
			groupField : groupField,
  		  		
  		proxy : {
  			type : proxyType,
  			url : cmdUrl,  			
  			extraParams : params,
  			reader : {
  				type : readerType,
  				root : root
  			}
  		},
  		  		
  		model : model
		});
		
		return commonDS;				
	}

/** add items on toolbar 
 * 
 * @param  obj
 * 		toolbar object
 * @param  text
 * @param  tooltip
 * @param  iconCls
 * 		icon of toolbar
 * @param  handler
 * 		handler function
 */	
	
	function addToolbarItem(obj, text, tooltip, iconCls, handler){		
		obj.add({text : text, tooltip : tooltip, iconCls : iconCls, handler : handler},'-');	
	}
		
/**	get textfield of ExtJS Form.
 *
 * @param  obj
 * @param  value
 * @param  fieldLabel
 * @param  labelAlign
 * @param  anchor
 * @param  allowBlank
 * @param  maxLength
 * @param  readonly
 * @param  fieldCls
 * 	view 화면시 field class.
 * 
 * @return {Ext.form.field.Text}	textField
 */
	
	function getTextField(obj, fieldLabel, labelAlign, anchor, allowBlank, maxLength, readOnly, fieldCls){
		
		var textField = {
			xtype : 'textfield',
			id : obj,
			name : obj,
			fieldLabel : fieldLabel,
			labelAlign : labelAlign,
			anchor : anchor,
			allowBlank : allowBlank,
			maxLength : maxLength,
			enforceMaxLength : true,
			readOnly : readOnly,
			fieldCls : fieldCls
		};
		
		return textField;
	}

/**	get hiddenField of ExtJS Form.
 * 
 * @param obj
 * 
 * @return hiddenField
 */
	
	function getHiddenField(obj){
		
		var hiddenField = {
			xtype : 'hiddenfield',
			id : obj,
			name : obj
		};
			
		return hiddenField;
	}

/* show ExtJS Window */
	
	function showWin(objId){		
		gCmpObj(objId).show();		
	}
	
/* close ExtJS Window */
	
	function closeWin(objId){		
		gCmpObj(objId).close();		
	}
		
/* response 의 responseText 를 decode 하여 return 한다. */
	
	function getDecodedRespTxt(responseText){
		return Ext.decode(responseText);
	}

/**	Ajax 를 통하여 Server Side 로 값을 통신한다.
 * 
 * @param  url
 * @param  params
 * @param  method
 * @param  action
 * 	action flag 로서 handlerSuccess 혹은 handlerFailure flag 로 이용됨.
 * @param  handlerSuccess
 * 	success 시 handler function.
 * @param  handlerFailure
 * 	failure 시 handler function.  
 */
	
	function callAjaxRequest(url, params, method, action, handlerSuccess, handlerFailure){
		
		Ext.Ajax.request({
			url : url,
			params : params,
			method : method,
			success : function(r,o){
				handlerSuccess = handlerSuccess+'(r,o,action)';				
				eval(handlerSuccess);
			}, failure : function(r,o){
				handlerFailure = handlerFailure+'(r,o,action)';
				eval(handlerFailure);
			}
		});

	}
	
/** Form 값을 Server Side 로 보낸다. 
 * 
 * @param  form
 * 	form panel 객체.
 * @param  url
 * @param  params
 * @param  method
 * @param  action
 * 	action flag 로서 handlerSuccess 혹은 handlerFailure flag 로 이용됨.
 * @param  handlerSuccess
 * 	success 시 handler function.
 * @param  handlerFailure
 * 	failure 시 handler function. 
 */	
	
	function callFormSubmitRequest(form, url, params, method, reader, action, handlerSuccess, handlerFailure){
		
		form.submit({
			url : url,
			params : params,
			method : method,
			reader : reader,
			success : function(f, a){
				handlerSuccess = handlerSuccess+'(f,a,action)';				
				eval(handlerSuccess);
			},
			failure : function(f, a){
				handlerFailure = handlerFailure+'(f,a,action)';
				eval(handlerFailure);
			}				
		});		
		
	}
	
/* Grid의 체크박스에서 checked 된 records 배열을 리턴한다. */
	
	function getGridSelectionModel(gridId){
		return gCmpObj(gridId).getSelectionModel().getSelection();
	}
	
/** ExtJS 의 Combobox 를 생성한다.
 * 
 * @param obj
 * @param renderTo
 * @param store
 * @param fieldLabel
 * @param displayField
 * @param valueField
 * @param allowBlank
 * @param queryMode
 * 	'local' 혹은 'remote'
 * @param labelWidth
 * 	width of label.
 * @param width
 * @param emptyText
 */
	
	function getCommonCombo(obj, renderTo, store, fieldLabel, displayField, valueField, value, allowBlank, queryMode, labelWidth, width, emptyText){
 
		var combo = Ext.create(EXTJS_COMBOBOX,{
			id : obj,
			name : obj,
			renderTo : renderTo,
			store : store,
			fieldLabel : fieldLabel,
			displayField : displayField,
			valueField : valueField,
			value : value,			
			allowBlank : allowBlank,
			queryMode : queryMode,
			editable : false,
			triggerAction : 'all',	
			labelWidth : labelWidth,
			width : width,
			emptyText : emptyText
		});
		
		return combo;
	}	

/** ExtJS 의 Window 를 생성한다.
 * 
 * @param obj
 * @param title
 * @param items
 * @param width
 * @param buttons
 * 
 */
		
	function getCommonWin(obj, title, items, width, buttons){
		
		Ext.create(EXTJS_WINDOW, {
			id : obj,
			layout : 'form',
			title : title,
			items : items,
			width : width,	
			autoHeight : true,
			bodyStyle : 'padding:5px;',
			plain : true,
			resizable : false,
			autoDestory : true,
			closable : false,
			buttons : buttons,			
			buttonAlign : 'center'
		});
		
	}